(function (global, factory) {
    "use strict"
    if (typeof module === "object" && typeof module.exports === "object") {
        module.exports = factory(global, true)
    } else if (typeof define === 'function' && define.amd) {
        define(function () { return factory(global) })
    } else {
        factory(global)
    }
})(typeof window !== "undefined" ? window : this, function (context) {
    const selector = function () {
        var ls = document.querySelectorAll(arguments[0]);
        if (!ls) return null;
        if (ls.length === 1) return ls[0]
        return ls
    }

    //默认配置
    const defaultConfig = {
        /*
            1: modal 模态框
            2: confirm 询问框
            3: alert 确认框
            4: toast 提示
            5: loading 加载
        */
        type: 1,
        shade: true,
        // shadeClose: true,
        // fixed: true,
        anim: 'scale', //默认动画类型
        title: '提示',
        titleStyle: '',
        btnItems: ['取消', '确定'],
        btnItemsStyle: ['', ''],
        btnBoxStyle: '',
        delay: 3 * 1000,
    };

    const extend = function (options) {
        const newobj = JSON.parse(JSON.stringify(defaultConfig))
        for (const i in options) {
            const value = options[i]
            if (value !== undefined || value !== null || value !== '') {
                newobj[i] = value
            }
        }
        return newobj
    }

    let index = 1

    const classes = ['nui-native-dialog']

    function NativeDialog(options) {
        var that = this;
        that.index = index++
        that.config = extend(options);
        that.view();
    }

    NativeDialog.prototype = {
        constructor: NativeDialog,
        view: function () {
            let that = this
            const config = that.config
            const layerbox = doc.createElement('dialog');
            that.id = layerbox.id = classes[0] + that.index;
            layerbox.setAttribute('class', [
                classes[0],
                classes[0] + (config.type || 0),
            ].join(' '));
            layerbox.setAttribute('index', index);
            const type = config.type

            const html = []

            //标题区域
            const title = function () {
                return '<h3 class="nui-native-dialog-hd" style="' + (config.titleStyle || '') + '">' + config.title + '</h3>'
            }

            if (type === 1 || type === 2 || type === 3) {
                html.push(title())
            }

            const body = function () {
                return '<div class="native-dialog-bd">'
                    + config.content
                    + '</div>'
            }

            html.push(body())

            //按钮区域
            const button = function () {
                const btnItemsStyle = Array.isArray(config.btnItemsStyle) ? config.btnItemsStyle : []
                let btns = Array.isArray(config.btnItems) ? config.btnItems : defaultConfig.btnItems

                



                btns = btns.map(function (btn, i) {
                    return '<button data-id="' + i + '" style="' + (btnItemsStyle[i] || '') + '">' + btn + '</button>'
                }).join('')
                return '<div class="nui-native-dialog-ft" style="' + (config.btnBoxStyle[i] || '') + '">' + btns + '</div>'
            }

            if (type === 1 || type === 2 || type === 3) {
                html.push(button())
            }
        }
    }

    context.nativeDialog = {
        v: '2.0',
        open: function (options) {
            var o = new NativeDialog(options)
            return o.index
        },
        // confirm: function (options) {
        //     options = options || {}
        //     options.type = 2
        //     var o = new NativeDialog(options)
        //     return o.index
        // },
        // alert: function (options) {
        //     options = options || {}
        //     options.type = 3
        //     options.btnItems = []
        //     var o = new NativeDialog(options)
        //     return o.index
        // },
        // toast: function (options) {
        //     options = options || {}
        //     options.type = 4
        //     var o = new NativeDialog(options)
        //     return o.index
        // },
        // loading: function (options) {
        //     options = options || {}
        //     options.type = 5
        //     var o = new NativeDialog(options)
        //     return o.index
        // },
        close: function (index) {
            var ibox = S('#' + classs[0] + index)[0];
            if (!ibox) return;
            ibox.innerHTML = '';
            doc.body.removeChild(ibox);
            clearTimeout(ready.timer[index]);
            delete ready.timer[index];
            typeof ready.end[index] === 'function' && ready.end[index]();
            delete ready.end[index];
        },

        //关闭所有弹层
        closeAll: function () {
            var boxs = doc[claname](classs[0]);
            for (var i = 0, len = boxs.length; i < len; i++) {
                layer.close((boxs[0].getAttribute('index') | 0));
            }
        }
    };
})